{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 清理资源\n",
    "\n",
    "本笔记本演示了如何清理在上一组笔记本中创建的所有资源。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import boto3\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Configure the SDK to Personalize:\n",
    "personalize = boto3.client('personalize')\n",
    "personalize_runtime = boto3.client('personalize-runtime')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%store -r"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请注意，这些步骤取决于每个步骤都已完全完成，然后另一个步骤才能起作用。 如果收到错误，请等待一两分钟，然后重试。\n",
    "\n",
    "## 清理活动（Campaigns）\n",
    "\n",
    "首先清理活动（Campaigns）。 您需要先删除Campaigns，然后才能删除它们所基于的Solutions版本。\n",
    "\n",
    "以下代码将删除您创建的Campaigns。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "personalize.delete_campaign(\n",
    "    campaignArn = userpersonalization_campaign_arn\n",
    ")\n",
    "personalize.delete_campaign(\n",
    "    campaignArn = sims_campaign_arn\n",
    ")\n",
    "personalize.delete_campaign(\n",
    "    campaignArn = rerank_campaign_arn\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 清理Event Trackers\n",
    "\n",
    "接下来，清理所有Event Trackers。 以下代码将删除您的Event Trackers。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "personalize.delete_event_tracker(\n",
    "    eventTrackerArn = event_tracker_arn\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 清理Filters\n",
    "\n",
    "接下来，清理所有Filters。 下面的代码遍历您创建的所有Filters并删除它们。 请注意，如果您收到关于Campaigns的错误，没关系，请等待2分钟，然后再次尝试下面的单元格。\n",
    "\n",
    "注意：在删除Campaigns之前，您将无法删除Filters，这可能需要几分钟的时间。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for filter_arn in meta_filter_arns:\n",
    "    personalize.delete_filter(\n",
    "        filterArn = filter_arn\n",
    "    )\n",
    "for filter_arn in interaction_filter_arns:\n",
    "    personalize.delete_filter(\n",
    "        filterArn = filter_arn\n",
    "    )\n",
    "for filter_arn in decade_filter_arns:\n",
    "    personalize.delete_filter(\n",
    "        filterArn = filter_arn\n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 清理Solutions\n",
    "\n",
    "接下来，清理Solutions。 下面的代码将删除创建的Solutions。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "personalize.delete_solution(\n",
    "    solutionArn = user_personalization_solution_arn\n",
    ")\n",
    "personalize.delete_solution(\n",
    "    solutionArn = sims_solution_arn\n",
    ")\n",
    "personalize.delete_solution(\n",
    "    solutionArn = rerank_solution_arn\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 清理Datasets\n",
    "\n",
    "接下来，清理Datasets。 下面的代码将删除items datasets和interactions datasets。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "personalize.delete_dataset(\n",
    "    datasetArn = items_dataset_arn\n",
    ")\n",
    "personalize.delete_dataset(\n",
    "    datasetArn = interactions_dataset_arn\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 清理Schemas\n",
    "\n",
    "接下来，清理Schemas。 您创建了 interaction schema 和 item schema，现在将其删除。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "personalize.delete_schema(\n",
    "    schemaArn = interaction_schema_arn\n",
    ")\n",
    "personalize.delete_schema(\n",
    "    schemaArn = itemmetadataschema_arn\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 清理Dataset Groups\n",
    "\n",
    "最后，清理Dataset Groups。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "personalize.delete_dataset_group(\n",
    "    datasetGroupArn = dataset_group_arn\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 清理S3存储桶和IAM Role\n",
    "\n",
    "首先删除IAM Role，然后清空存储桶，最后删除存储桶。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "iam = boto3.client('iam')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "标识要删除的IAM Role的名称。\n",
    "\n",
    "您无法删除仍附加有策略的IAM Role。 因此，在确定了相关IAM Role后，让我们列出该IAM Role的附加策略。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "iam.list_attached_role_policies(\n",
    "    RoleName = role_name\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "您需要使用以下代码分离以上结果中的策略。 对每个附加策略重复此操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "iam.detach_role_policy(\n",
    "    RoleName = role_name,\n",
    "    PolicyArn = \"arn:aws-cn:iam::aws:policy/service-role/AmazonPersonalizeFullAccess\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "iam.detach_role_policy(\n",
    "    RoleName = role_name,\n",
    "    PolicyArn = 'arn:aws-cn:iam::aws:policy/AmazonS3FullAccess'\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "最后，您应该能够删除IAM Role。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "iam.delete_role(\n",
    "    RoleName = role_name\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要删除S3存储桶，首先需要将其清空。 删除S3存储桶的最简单方法是，在亚马逊云科技控制台中导航到S3，删除存储桶中的对象，然后删除S3存储桶本身。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda_python3",
   "language": "python",
   "name": "conda_python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
